//
//  DogParkClient.m
//  HundPark
//
//  Created by MacBooken Rolle on 5/8/13.
//  Copyright (c) 2013 ARV IT. All rights reserved.
//

#import "DogParkClient.h"

@implementation DogParkClient

static DogParkClient *_sharedClient = nil;


#define SERVERURL @"http://www.hundapp.byethost8.com/"
#define GETCITY @"HundApp.php?method=getCity"
#define GETCITY2 @"HundApp.php?method=getCity2"
#define CHECKIN @"HundApp.php?method=checkIn"
#define CHECKSTATUS @"HundApp.php?method=checkStatus"

#pragma mark -
#pragma mark Shared Instance
+ (DogParkClient *)sharedClient {
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:SERVERURL]];
    });
    
    return _sharedClient;
}

#pragma mark -
#pragma mark Private Methods
- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    
    if (self) {
        // Register HTTP Operation Class
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
        
        // Accept HTTP Header
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }
    
    return self;
}

- (void)updateStatusAtPark:(NSDictionary *) parkInfo{
    NSMutableURLRequest *request = [self requestWithMethod:@"POST"
                                                      path:CHECKIN
                                                parameters:parkInfo];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                         JSONRequestOperationWithRequest:request
                                         success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject)
                                         {
                                             if([self.delegate respondsToSelector:@selector(dogParkClient:didUpdateStatus:)])
                                                 [self.delegate dogParkClient:self didUpdateStatus:responseObject];
                                         }
                                         failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                         {
                                             if([self.delegate respondsToSelector:@selector(dogParkClient:didFailWithError:)])
                                                 [self.delegate dogParkClient:self didFailWithError:error];
                                         }];
	[operation start];

}


- (void)checkStatusForPark:(NSDictionary *) parkInfo{
    
    NSMutableURLRequest *request = [self requestWithMethod:@"POST"
                                                      path:CHECKSTATUS
                                                parameters:parkInfo];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                         JSONRequestOperationWithRequest:request
                                         success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject)
                                         {
                                             if([self.delegate respondsToSelector:@selector(dogParkClient:didUpdateStatus:)])
                                                  [self.delegate dogParkClient:self didCheckStatus:responseObject];
                                         }
                                         failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                         {
                                             if([self.delegate respondsToSelector:@selector(dogParkClient:didFailWithError:)])
                                                 [self.delegate dogParkClient:self didFailWithError:error];
                                         }];
	[operation start];
}

@end
